package cn.px.rest.controller.purchase;

import cn.px.common.bean.AbstractPageResultBean;
import cn.px.common.util.BeanConverterUtil;
import cn.px.common.util.R;
import cn.px.common.validation.group.AddGroup;
import cn.px.common.validation.group.UpdateGroup;
import cn.px.dao.dto.purchase.PurchaseDemandPlanListAddDTO;
import cn.px.dao.entity.purchase.PurchaseDemandPlanListAdd;
import cn.px.dao.util.ExcelUtil;
import cn.px.dao.vo.export.purchase.ExportPurchaseDemandPlanListAddVO;
import cn.px.dao.vo.query.purchase.PurchaseDemandPlanListAddVO;
import cn.px.rest.log.annotation.SysLog;
import cn.px.service.purchase.PurchaseDemandPlanListAddService;
import lombok.AllArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @Description 采购总需求计划增补明细 控制器
 * @author 品讯科技
 * @date 2024-08
 * @Version Copyright (c) 2020,北京乾元坤和科技有限公司 All rights reserved.
 */
@RestController
@AllArgsConstructor
@RequestMapping("/purchase/purchase-demand-plan-list-adds")
public class PurchaseDemandPlanListAddController {

    private final PurchaseDemandPlanListAddService purchaseDemandPlanListAddService;

    /**
     * 通过id查询单条记录
     *
     * @param id 主键
     * @return R
     */
    @GetMapping("/{id}")
    public R<PurchaseDemandPlanListAdd> getById(@PathVariable("id") String id) {
        return new R<>(purchaseDemandPlanListAddService.getModelById(id));
    }

    /**
     * 新增记录
     *
     * @param purchaseDemandPlanListAddDTO 采购总需求计划增补明细DTO
     * @return R
     */
    @SysLog("新增采购总需求计划增补明细")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('purchase-purchase-demand-plan-list_adds-add')")
    public R save(@Validated({AddGroup.class}) @RequestBody PurchaseDemandPlanListAddDTO purchaseDemandPlanListAddDTO) {
        return new R<>(purchaseDemandPlanListAddService.saveModelByDTO(purchaseDemandPlanListAddDTO));
    }

    /**
     * 修改记录
     *
     * @param purchaseDemandPlanListAddDTO 采购总需求计划增补明细DTO
     * @return R
     */
    @SysLog("修改采购总需求计划增补明细")
    @PutMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('purchase-purchase-demand-plan-list_adds-edit')")
    public R update(@PathVariable("id") String id, @Validated({UpdateGroup.class}) @RequestBody PurchaseDemandPlanListAddDTO purchaseDemandPlanListAddDTO) {
        purchaseDemandPlanListAddService.modModelByDTO(purchaseDemandPlanListAddDTO);
        return new R<>();
    }

    /**
     * 通过id删除一条记录
     *
     * @param id 主键
     * @return R
     */
    @SysLog("删除采购总需求计划增补明细")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('purchase-purchase-demand-plan-list_adds-del')")
    public R removeById(@PathVariable String id) {
        purchaseDemandPlanListAddService.deleteByPrimaryKey(id);
        return new R<>();
    }

    /**
     * 批量记录
     *
     * @param ids 主键
     * @return R
     */
    @SysLog("批量删除采购总需求计划增补明细")
    @DeleteMapping("")
    @PreAuthorize("@pms.hasPermission('purchase-purchase-demand-plan-list_adds-del')")
    public R removeIds(@RequestBody List<String> ids) {
        purchaseDemandPlanListAddService.batchDelete(ids);
        return new R<>();
    }

    /**
     * 获取分页
     *
     * @param pageNum                      当前页码
     * @param pageSize                     每页条数
     * @param purchaseDemandPlanListAddDTO 采购总需求计划增补明细DTO
     * @return R
     */
    @GetMapping("/page")
    public R<AbstractPageResultBean<PurchaseDemandPlanListAddVO>> query(PurchaseDemandPlanListAddDTO purchaseDemandPlanListAddDTO, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "20") int pageSize) {
        return new R<>(purchaseDemandPlanListAddService.selectPageList(purchaseDemandPlanListAddDTO, pageNum, pageSize));
    }

    /**
     * 获取列表
     *
     * @param purchaseDemandPlanListAddDTO 采购总需求计划增补明细DTO
     * @return R
     */
    @GetMapping("")
    public R query(PurchaseDemandPlanListAddDTO purchaseDemandPlanListAddDTO) {
        return new R<>(purchaseDemandPlanListAddService.getModelListByDTO(purchaseDemandPlanListAddDTO));
    }

    /**
     * 采购总需求计划增补明细导出
     *
     * @param purchaseDemandPlanListAddDTO 采购总需求计划增补明细DTO
     * @return R
     */
    @GetMapping("/export")
    @SysLog("采购总需求计划增补明细导出")
    @PreAuthorize("@pms.hasPermission('purchase-purchase-demand-plan-list_adds-export')")
    public void export(HttpServletResponse response, PurchaseDemandPlanListAddDTO purchaseDemandPlanListAddDTO) {
        List<PurchaseDemandPlanListAdd> list = purchaseDemandPlanListAddService.getModelListByDTO(purchaseDemandPlanListAddDTO);
        ExcelUtil.exportResponseDict(response, ExportPurchaseDemandPlanListAddVO.class, BeanConverterUtil.copyListProperties(list, ExportPurchaseDemandPlanListAddVO.class), "采购总需求计划增补明细");
    }

    /**
     * 采购总需求计划明细导入
     * 导入某个计划的需求计划明细
     *
     * @return R
     */
    @PostMapping("/import")
    @SysLog("采购总需求计划明细导入")
    public R importExcel(@RequestParam("file") MultipartFile file, Integer type) throws Exception {
        return R.success(purchaseDemandPlanListAddService.importList(file, type));
    }

}
